#include "basic.h"
#include "io.h"
#include "(super)peer.h"
#include "boot_methods.h"
#include "List/simclist_impl.h"

char* BOOT_IP = "127.0.0.1";

int main(int argc, char **argv)
{
	in_addr_t my_ip;

    if(init_directory()<0) return 0;
    int exitCode = -1;
	retryBoot:
	{
		int n=0;
		check_server_ip();
		sleep(3);
		int is_superpeer=start_boot(&my_ip, &exitCode,&n);
		if(is_superpeer==1) superpeer_routine(my_ip);  // !=0 --> MEMORIZZO IL MIO INDIRIZZO
		else {
			struct in_addr addr;
			addr.s_addr=choose_superpeer(0);
			printf("Assegnato a superpeer: %s\n",inet_ntoa(addr));
			exitCode = peer_routine(addr.s_addr,my_ip);
			if (exitCode != -1) {
				addr.s_addr=exitCode;
				printf("exitCode e': %s\n",inet_ntoa(addr));
			}
			else
				printf("rieffettuo il boot normale");
		}
	}
	if (exitCode != 0 || exitCode == NULL)
		goto retryBoot;

	return 1;
}









